package com.example.demo.configuration;

import javax.sql.DataSource;

import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@SpringBootConfiguration
public class MybatisSpringConfig {

//	@Bean
//	@ConditionalOnMissingBean // 当容器里没有指定的Bean的情况下创建该对象
//	public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception {
//		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//		// 设置数据源
//		sqlSessionFactoryBean.setDataSource(dataSource);
//
//		// 添加驼峰命名法,映射
//		Configuration configuration = new Configuration();
//		configuration.setMapUnderscoreToCamelCase(true);
//		sqlSessionFactoryBean.setConfiguration(configuration);
//
//		// 设置mybatis的主配置文件
//		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
////		Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/mybatis-config.xml");
////		sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
//		Resource[] resources = resolver.getResources("classpath:mybatis/mapper/*.xml");
//		sqlSessionFactoryBean.setMapperLocations(resources);
//		// 设置别名包
//		sqlSessionFactoryBean.setTypeAliasesPackage("com.example.demo.pojo");
//		return sqlSessionFactoryBean;
//	}

}